1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#![allow(non_camel_case_types, non_snake_case)]

use crate::co;
use crate::decl::*;
use crate::kernel::{ffi_types::*, privs::*};
use crate::ole::privs::*;
use crate::prelude::*;
use crate::vt::*;

/// [`IMediaControl`](crate::IMediaControl) virtual table.
#[repr(C)]
pub struct IMediaControlVT {
	pub IDispatchVT: IDispatchVT,
	pub Run: fn(COMPTR) -> HRES,
	pub Pause: fn(COMPTR) -> HRES,
	pub Stop: fn(COMPTR) -> HRES,
	pub GetState: fn(COMPTR, i32, *mut u32) -> HRES,
	pub RenderFile: fn(COMPTR, PSTR) -> HRES,
	pub AddSourceFilter: fn(COMPTR, PSTR, *mut COMPTR) -> HRES,
	pub GetFilterCollection: fn(COMPTR, *mut COMPTR) -> HRES,
	pub GetRegFilterCollection: fn(COMPTR, *mut COMPTR) -> HRES,
	pub StopWhenReady: fn(COMPTR) -> HRES,
}

com_interface! { IMediaControl: "56a868b1-0ad4-11ce-b03a-0020af0ba770";
	/// [`IMediaControl`](https://learn.microsoft.com/en-us/windows/win32/api/control/nn-control-imediacontrol)
	/// COM interface over [`IMediaControlVT`](crate::vt::IMediaControlVT).
	///
	/// Automatically calls
	/// [`IUnknown::Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
	/// when the object goes out of scope.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*};
	///
	/// let graph_builder: w::IGraphBuilder; // initialized somewhere
	/// # let graph_builder = unsafe { w::IGraphBuilder::null() };
	///
	/// let media_control = graph_builder
	///     .QueryInterface::<w::IMediaControl>()?;
	/// # w::HrResult::Ok(())
	/// ```
}

impl oleaut_IDispatch for IMediaControl {}
impl dshow_IMediaControl for IMediaControl {}

/// This trait is enabled with the `dshow` feature, and provides methods for
/// [`IMediaControl`](crate::IMediaControl).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait dshow_IMediaControl: oleaut_IDispatch {
	/// [`IMediaControl::AddSourceFilter`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-addsourcefilter)
	/// method.
	#[must_use]
	fn AddSourceFilter(&self, file_name: &str) -> HrResult<IDispatch> {
		let mut queried = unsafe { IDispatch::null() };
		ok_to_hrresult(
			unsafe {
				(vt::<IMediaControlVT>(self).AddSourceFilter)(
					self.ptr(),
					WString::from_str(file_name).as_mut_ptr(), // BSTR
					queried.as_mut(),
				)
			},
		).map(|_| queried)
	}

	/// [`IMediaControl::GetState`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-getstate)
	/// method.
	#[must_use]
	fn GetState(&self,
		ms_timeout: Option<i32>,
	) -> HrResult<co::FILTER_STATE>
	{
		let mut state = co::FILTER_STATE::default();
		ok_to_hrresult(
			unsafe {
				(vt::<IMediaControlVT>(self).GetState)(
					self.ptr(),
					ms_timeout.unwrap_or(INFINITE as _),
					state.as_mut(),
				)
			},
		).map(|_| state)
	}

	/// [`IMediaControl::Pause`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-pause)
	/// method.
	fn Pause(&self) -> HrResult<bool> {
		okfalse_to_hrresult(
			unsafe { (vt::<IMediaControlVT>(self).Pause)(self.ptr()) },
		)
	}

	fn_com_bstr_set! { RenderFile: IMediaControlVT, file_name;
		/// [`IMediaControl::RenderFile`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-renderfile)
		/// method.
	}

	/// [`IMediaControl::Run`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-run)
	/// method.
	fn Run(&self) -> HrResult<bool> {
		okfalse_to_hrresult(
			unsafe { (vt::<IMediaControlVT>(self).Run)(self.ptr()) },
		)
	}

	fn_com_noparm! { Stop: IMediaControlVT;
		/// [`IMediaControl::Stop`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-stop)
		/// method.
	}

	/// [`IMediaControl::StopWhenReady`](https://learn.microsoft.com/en-us/windows/win32/api/control/nf-control-imediacontrol-stopwhenready)
	/// method.
	fn StopWhenReady(&self) -> HrResult<bool> {
		okfalse_to_hrresult(
			unsafe { (vt::<IMediaControlVT>(self).StopWhenReady)(self.ptr()) },
		)
	}
}